import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';

class SettingsService {
  static const String _darkModeKey = 'dark_mode';
  static const String _notificationsKey = 'notifications_enabled';
  static const String _backupReminderKey = 'backup_reminder';
  static const String _languageKey = 'language';
  static const String _themeColorKey = 'theme_color';

  static SettingsService? _instance;
  final SharedPreferences _prefs;

  SettingsService._(this._prefs);

  static Future<SettingsService> getInstance() async {
    if (_instance == null) {
      final prefs = await SharedPreferences.getInstance();
      _instance = SettingsService._(prefs);
    }
    return _instance!;
  }

  // Theme settings
  bool get isDarkMode => _prefs.getBool(_darkModeKey) ?? false;

  Future<void> setDarkMode(bool value) async {
    await _prefs.setBool(_darkModeKey, value);
  }

  Color get themeColor {
    final colorValue = _prefs.getInt(_themeColorKey) ?? Colors.blue.value;
    return Color(colorValue);
  }

  Future<void> setThemeColor(Color color) async {
    await _prefs.setInt(_themeColorKey, color.value);
  }

  // Notification settings
  bool get notificationsEnabled => _prefs.getBool(_notificationsKey) ?? true;

  Future<void> setNotificationsEnabled(bool value) async {
    await _prefs.setBool(_notificationsKey, value);
  }

  // Backup settings
  bool get backupReminderEnabled => _prefs.getBool(_backupReminderKey) ?? true;

  Future<void> setBackupReminderEnabled(bool value) async {
    await _prefs.setBool(_backupReminderKey, value);
  }

  // Language settings
  String get language => _prefs.getString(_languageKey) ?? 'zh_CN';

  Future<void> setLanguage(String languageCode) async {
    await _prefs.setString(_languageKey, languageCode);
  }

  // Reset all settings to defaults
  Future<void> resetToDefaults() async {
    await Future.wait([
      _prefs.setBool(_darkModeKey, false),
      _prefs.setBool(_notificationsKey, true),
      _prefs.setBool(_backupReminderKey, true),
      _prefs.setString(_languageKey, 'zh_CN'),
      _prefs.setInt(_themeColorKey, Colors.blue.value),
    ]);
  }

  // Export settings to JSON
  Map<String, dynamic> toJson() {
    return {
      'darkMode': isDarkMode,
      'notificationsEnabled': notificationsEnabled,
      'backupReminderEnabled': backupReminderEnabled,
      'language': language,
      'themeColor': themeColor.value,
    };
  }

  // Import settings from JSON
  Future<void> fromJson(Map<String, dynamic> json) async {
    await Future.wait([
      setDarkMode(json['darkMode'] ?? false),
      setNotificationsEnabled(json['notificationsEnabled'] ?? true),
      setBackupReminderEnabled(json['backupReminderEnabled'] ?? true),
      setLanguage(json['language'] ?? 'zh_CN'),
      setThemeColor(Color(json['themeColor'] ?? Colors.blue.value)),
    ]);
  }

  // Listen to settings changes
  Stream<void> get onSettingsChanged {
    return Stream.periodic(Duration(seconds: 1)).asyncMap((_) async {
      return _prefs.reload();
    });
  }
}
